package com.lzy.srb.oss.controller.api;

import com.lzy.common.result.R;
import com.lzy.srb.oss.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Api("阿里云文件管理")
@RestController
@RequestMapping("/api/oss/file")
//@CrossOrigin
public class FileController {

    @Autowired
    FileService fileService;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file,
                    @RequestParam("module") String module) throws IOException {

        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String url = fileService.upload(inputStream, module, originalFilename);
        return R.ok().message("文件上传成功").data("url",url);
    }

    @ApiOperation("删除上传文件")
    @DeleteMapping("/remove")
    public R remove(@RequestParam("url") String url){
        fileService.remove(url);
        return R.ok().message("删除成功");
    }


}
